package com.lepu.app.main.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.app.utils.AppEnterTongJi;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragment;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.PopUtil;
import com.eyzhs.app.R;
import com.lepu.app.fun.grow.GrowCloudUploadActivity;
import com.lepu.app.fun.grow.GrowHomeShareActivity;
import com.lepu.app.fun.grow.fragment.FragmentGrow;
import com.lepu.app.fun.grow.fragment.FragmentHotPhoto;
import com.lepu.app.fun.grow.setting.GrowSettingActivity;
import com.lepu.app.main.activity.MainTabActivity;

import java.util.ArrayList;

public class MainTabFragmentGrow extends BaseFragment implements AsyncRequest {

	private static final int MSG_TEST = 1;

	private View mMainView = null;
	private boolean mInitDataSuccess = false;// 是否加载完数据

	private ViewPager mViewPager = null;
	private ImageView mSettingImageView = null;
	private LinearLayout mTabLayoutOne = null;
	private LinearLayout mTabLayoutTwo = null;

	private int mCurrentTab = 0;
	public boolean mRefreshFlag = true;

	FragmentHotPhoto mFragmentHotPhoto = null;

	@SuppressLint("InflateParams")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mMainView = LayoutInflater.from(getActivity()).inflate(
				R.layout.main_tab_fragment_grow, null, false);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		ViewGroup p = (ViewGroup) mMainView.getParent();
		if (p != null) {
			p.removeAllViewsInLayout();
		}
		return mMainView;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mInitDataSuccess = false;
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) {
			if (!mInitDataSuccess || mRefreshFlag) {

				// 第一次进入页面获取数据
				mRefreshFlag = false;
				mInitDataSuccess = true;

				if(mCurrentTab == 1 && mFragmentHotPhoto != null){
					mFragmentHotPhoto.request(false);
					return ;
				}

				init();

				// 第一次进入页面获取数据
				mViewPager.setCurrentItem(0);
				updateTabLine(0);
			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	/*
	 * 初始化ui控件，不要初始化数据！！！！
	 *
	 * 初始化控件，需要用mMainView.findViewById 不能使用findViewById，否则程序会闪退的
	 */
	private void init() {
		// content
		ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
		FragmentGrow fragmentGrow = new FragmentGrow();
		mFragmentHotPhoto = new FragmentHotPhoto();

		fragmentList.add(fragmentGrow);
		fragmentList.add(mFragmentHotPhoto);

		mViewPager = (ViewPager) mMainView.findViewById(R.id.growViewPager);
		mViewPager.setAdapter(new FragmentPageAdapter(getActivity()
				.getSupportFragmentManager(), fragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());

		// tab
		mTabLayoutOne = (LinearLayout) mMainView.findViewById(R.id.tabLayout1);
		mTabLayoutTwo = (LinearLayout) mMainView.findViewById(R.id.tabLayout2);
		mSettingImageView = (ImageView) mMainView
				.findViewById(R.id.settingImageView);

		mTabLayoutOne.setOnClickListener(mViewClickListener);
		mTabLayoutTwo.setOnClickListener(mViewClickListener);
		mSettingImageView.setOnClickListener(mViewClickListener);

	}

	public void refresh()
	{
		if(mCurrentTab == 1 && mFragmentHotPhoto != null){
			mFragmentHotPhoto.request(false);
		}
	}

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_TEST:

				break;

			default:
				break;
			}
		};
	};

	View.OnClickListener mViewClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tabLayout1:
				if (mCurrentTab == 0)
					return;

				mViewPager.setCurrentItem(0,false);
				updateTabLine(0);
				break;
			case R.id.tabLayout2:

				if (mCurrentTab == 1)
					return;

				mViewPager.setCurrentItem(1,false);
				updateTabLine(1);
				break;
			case R.id.settingImageView:

				// showPopMainRight(v);

				Intent settingIntent = new Intent(getActivity(),
						GrowSettingActivity.class);
				MainTabActivity.getInstance()
						.startActivity(settingIntent, true);
				break;

			default:
				break;
			}
		}
	};

	private void updateTabLine(int currentIndex) {
		if (currentIndex == 0) {
			mTabLayoutOne.setSelected(true);
			mTabLayoutTwo.setSelected(false);
			
			AppEnterTongJi.clickEvent(23, "");
		} else {
			mTabLayoutOne.setSelected(false);
			mTabLayoutTwo.setSelected(true);
			
			AppEnterTongJi.clickEvent(24, "");
		}
	}

	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mCurrentTab = index;
			mViewPager.setCurrentItem(index,false);
			updateTabLine(index);
		}
	}

	@SuppressLint("InflateParams")
	private void showPopMainRight(View v) {
		View view = LayoutInflater.from(getActivity()).inflate(
				R.layout.grow_pop_main_right, null);
		LinearLayout popItemLayoutShare = (LinearLayout) view
				.findViewById(R.id.popItemLayoutShare);
		LinearLayout popItemLayoutUpdate = (LinearLayout) view
				.findViewById(R.id.popItemLayoutUpdate);
		LinearLayout popItemLayoutSetting = (LinearLayout) view
				.findViewById(R.id.popItemLayoutSetting);

		popItemLayoutShare.setOnClickListener(mPopRightClickListener);
		popItemLayoutUpdate.setOnClickListener(mPopRightClickListener);
		popItemLayoutSetting.setOnClickListener(mPopRightClickListener);

		PopUtil.getInstance().showPopWindow(getActivity(), v, view, 0, null,
				true);
	}

	private View.OnClickListener mPopRightClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			MainTabActivity instance = MainTabActivity.getInstance();

			int rid = v.getId();
			switch (rid) {
			case R.id.popItemLayoutShare:
				Intent homeShareIntent = new Intent(instance,
						GrowHomeShareActivity.class);
				instance.startActivity(homeShareIntent, true);
				break;
			case R.id.popItemLayoutUpdate:

				boolean flag = MainTabActivity.jumpLogin(MainTabActivity
						.getInstance());
				if (!flag) {
					Intent updateIntent = new Intent(instance,
							GrowCloudUploadActivity.class);
					instance.startActivity(updateIntent, true);
				}
				break;
			case R.id.popItemLayoutSetting:

				Intent settingIntent = new Intent(instance,
						GrowSettingActivity.class);
				instance.startActivity(settingIntent, true);
				break;

			default:
				break;
			}

			PopUtil.getInstance().dismissPopWindow();
		}
	};

	@Override
	public void RequestComplete(Object object, Object data) {
		
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
	}
}
